Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Accessing nth table and counting rows with pupeteer in javascript error: "failed to find element matching selector "table:nth-of-type(2) > tr""

I am trying to programmatically count the rows in tables from the page https://rarity.tools/upcoming/ in javascript. Since the site loads through javascript, I've been using puppeteer. The site has multiple tables (4 total) and I would like to be able to reference each table and check how many rows they have.

I've tried to use nth-of-type to select the second, third, and fourth table, but I receive the error:

"Error: Error: failed to find element matching selector "table:nth-of-type(2) > tr""

Removing > tr returns a similar error:

"Error: Error: failed to find element matching selector "table:nth-of-type(2)""

I've tried using nth-child as well.

Code:

const puppeteer = require('puppeteer');

let fetch = async () => {

    try {
        // Puppeteer initialization
        const browser = await puppeteer.launch({ headless: true, defaultViewport: null });
        const [page] = await browser.pages();
        await page.goto('https://rarity.tools/upcoming/');
        await page.waitForTimeout(2500) // Timeout so page can load actual content 

        const numTables = await page.$$eval('table', el => el.length) - 1; // Rarity.tools has a table for each month of data. Each table must be looped through.
        // There is an extra table for upcoming "To Be Announced", there is a -1 to subtract that table from tables being looped through. 

        for (let j = 1; j < numTables + 1; j++) {
            const tableLength = await page.$eval(`table:nth-of-type(${j}) > tr`, el => el.length)
            console.log(tableLength)
        }

    }

    catch (error) {
        console.log(error)
    }
}


fetch();

Utilizing the following instead doesn't return an error but instead returns 0 for the second and third table, even though I can see the HTML of the site I'm scraping has table rows under the second and third table.

const tableLength = await page.$$eval(`table:nth-of-type(${j}) > tr`, el => el.length)

However, the code above with $$eval returns the correct value for the first table.

Please let me know if I need to provide more information.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda